#include "combat.h"
#include "ui_combat.h"
#include<QDebug>
#include<QEventLoop>

Combat::Combat(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Combat)
{
    ui->setupUi(this);
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
    this->simple=false;
    this->normal=false;
    this->hard=false;
    this->pvp=false;
    this->pve=false;
    this->isOk=false;
    this->isPosition=false;
    this->result=-1;
    this->cnt=0;
    this->pve_b=false;
}

Combat::~Combat()
{
    delete ui;
}

void Combat::on_ret_clicked()
{
    emit ret();
}

void Combat::on_start_clicked()
{
    if(((simple||normal||hard)&&pve)||pvp){
        n=(qrand()%31+6)*2;
        wid=1500/(n+4);
        maze.init(n);
        maze.mapBuid();
        person1=maze.map[2][2];
        person2=maze.map[2][n];
        result=-1;
        isOk=true;
        update();
        if(pve){
            pve_b=true;
            setStPEdP(person2,maze.map[n][2]);
            pvePerson2();
        }
    }
}
void Combat::paintEvent(QPaintEvent *){
    QPainter painter(this);
    QPixmap pixmap;
    pixmap.load(":/rec/260.png");
    //设置反锯齿模式
     painter.setRenderHint(QPainter::Antialiasing, true);
     if(isOk){
         if(result==-1){
             for(int i=0;i<n+3;i++){
                 for(int j=0;j<n+3;j++){
                     if(maze.map[i][j].flag==-1||maze.map[i][j].flag==0){
                         //墙
                         pixmap.load(":/rec/wall.jpeg");
                         painter.drawPixmap(i*wid, j*wid, wid, wid,pixmap);
                     }else{
                        painter.setBrush(QColor(255, 255, 255));
                        painter.drawRect(i*wid, j*wid, wid, wid);
                     }
                 }
             }
             pixmap.load(":/rec/home.jpeg");
             painter.drawPixmap(2*wid, 2*wid, wid, wid,pixmap);
             pixmap.load(":/rec/end.jpg");
             painter.drawPixmap(n*wid, n*wid, wid, wid,pixmap);
             pixmap.load(":/rec/person2_home.webp");
             painter.drawPixmap(2*wid, n*wid, wid, wid,pixmap);
             pixmap.load(":/rec/home2.jpg");
             painter.drawPixmap(n*wid, 2*wid, wid, wid,pixmap);
             if(isPosition){
                 pixmap.load(":/rec/260.png");
                 painter.drawPixmap(person1.x*wid, person1.y*wid, wid, wid,pixmap);
                 pixmap.load(":/rec/p2.webp");
                 painter.drawPixmap(person2.x*wid, person2.y*wid, wid, wid,pixmap);
                 isPosition=false;
             }
         }else if(result==0){//平局
             pixmap.load(":/rec/result0.webp");
             painter.drawPixmap(0,0,1300,1300,pixmap);

         }else if(result==1){//person1赢
             pixmap.load(":/rec/result1.jpg");
             painter.drawPixmap(0,0,1300,1300,pixmap);

         }else if(result==2){//peison2 赢
             pixmap.load(":/rec/result2.jpg");
             painter.drawPixmap(0,0,1300,1300,pixmap);
         }

     }


}
void Combat::pvePerson2(){
    if(result!=-1||pvp){
        return;
    }
    if(pve_b){
        for(int i=2;i<n+1;i++){
            for(int j=2;j<n+1;j++){
                maze.map[i][j].flagNext=-1;
            }
        }
        if(route.size()){
            route.clear();
        }
        maze.map[this->stP.x][this->stP.y].flagNext=1;
        route.append(stP);
        pve_b=false;
    }
     cnt=route.size();
   if(route[cnt-1].x!=edP.x || route[cnt-1].y!=edP.y){
     if((maze.map[route[cnt-1].x+1][route[cnt-1].y].flagNext==-1) && (maze.map[route[cnt-1].x+1][route[cnt-1].y].flag==1)){
         route.append(maze.map[route[cnt-1].x+1][route[cnt-1].y]);
         maze.map[route[cnt-1].x+1][route[cnt-1].y].flagNext=1;
     }else if((maze.map[route[cnt-1].x][route[cnt-1].y+1].flagNext==-1) && (maze.map[route[cnt-1].x][route[cnt-1].y+1].flag==1)){
         route.append(maze.map[route[cnt-1].x][route[cnt-1].y+1]);
         maze.map[route[cnt-1].x][route[cnt-1].y+1].flagNext=1;
     }else if((maze.map[route[cnt-1].x-1][route[cnt-1].y].flagNext==-1) && (maze.map[route[cnt-1].x-1][route[cnt-1].y].flag==1)){
         route.append(maze.map[route[cnt-1].x-1][route[cnt-1].y]);
         maze.map[route[cnt-1].x-1][route[cnt-1].y].flagNext=1;
     }else if((maze.map[route[cnt-1].x][route[cnt-1].y-1].flagNext==-1) && (maze.map[route[cnt-1].x][route[cnt-1].y-1].flag == 1)){
         route.append(maze.map[route[cnt-1].x][route[cnt-1].y-1]);
         maze.map[route[cnt-1].x][route[cnt-1].y-1].flagNext=1;
     }else{
          route.removeAt(cnt-1);
     }
     person2=route[route.size()-1];
     pveSleep(msec);
     if(person1.x==n&&person1.y==n&&(person2.x!=n||person2.y!=2)){
         result=1;
     }else if(person1.x==n&&person1.y==n&&person2.x==n&&person2.y==2){
         result=0;
     }else if(person2.x==n&&person2.y==2&&(person1.x!=n||person1.y!=n)){
         result=2;
     }

     isPosition=true;
     update();
     pvePerson2();
   }
}
void Combat::pveSleep(int msec){
    QTime dieTime = QTime::currentTime().addMSecs(msec);
    while( QTime::currentTime() <dieTime ){
      QCoreApplication::processEvents(QEventLoop::AllEvents, 2000);
    }
}
void Combat::keyPressEvent(QKeyEvent *e){
    //person1
    //向上移动 w
    if(e->key()==0x57){
        if(maze.map[person1.x][person1.y-1].flag==1){
            person1.y--;
        }

    }
    //向右移动 d
    if(e->key()==0x44){
        if(maze.map[person1.x+1][person1.y].flag==1){
            person1.x++;
        }
    }
    //向下移动 s
    if(e->key()==0x53){
        if(maze.map[person1.x][person1.y+1].flag==1){
            person1.y++;
        }
    }
    //向左移动 a
    if(e->key()== 0x41){
        if(maze.map[person1.x-1][person1.y].flag==1){
            person1.x--;
        }
    }
    //person2
    //向上移动 i
    if(e->key()==0x49){
        if(maze.map[person2.x][person2.y-1].flag==1){
            person2.y--;
        }

    }
    //向右移动 l
    if(e->key()==0x4c){
        if(maze.map[person2.x+1][person2.y].flag==1){
            person2.x++;
        }
    }
    //向下移动 k
    if(e->key()==0x4b){
        if(maze.map[person2.x][person2.y+1].flag==1){
            person2.y++;
        }
    }
    //向左移动 j
    if(e->key()== 0x4a){
        if(maze.map[person2.x-1][person2.y].flag==1){
            person2.x--;
        }
    }
    if(person1.x==n&&person1.y==n&&(person2.x!=n||person2.y!=2)){
        result=1;
    }else if(person1.x==n&&person1.y==n&&person2.x==n&&person2.y==2){
        result=0;
    }else if(person2.x==n&&person2.y==2&&(person1.x!=n||person1.y!=n)){
        result=2;
    }
    isPosition=true;
    update();
}

void Combat::on_simple_clicked()
{
    this->simple=true;
    this->normal=false;
    this->hard=false;
    msec=1000;
}

void Combat::on_normal_clicked()
{
    this->simple=false;
    this->normal=true;
    this->hard=false;
    msec=500;
}

void Combat::on_hard_clicked()
{
    this->simple=false;
    this->normal=false;
    this->hard=true;
    msec=100;
}

void Combat::on_pvp_clicked()
{
    this->pvp=true;
    this->pve=false;
    ui->simple->setEnabled(false);
    ui->normal->setEnabled(false);
    ui->hard->setEnabled(false);

}

void Combat::on_pve_clicked()
{
    this->pvp=false;
    this->pve=true;
    ui->simple->setEnabled(true);
    ui->normal->setEnabled(true);
    ui->hard->setEnabled(true);

}

